using System;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Drawing;

namespace Weather
{
    public class LoginForm : Form
    {
        private TextBox txtUsername;
        private TextBox txtPassword;
        private Button btnLogin;
        private Button btnRegister;
        private Label lblTitle;

        public LoginForm()
        {
            InitializeComponents();
        }

        private void InitializeComponents()
        {
            this.Size = new Size(400, 300);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "登录";
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;

            // 标题
            lblTitle = new Label
            {
                Text = "心情日记",
                Location = new Point(0, 30),
                Size = new Size(400, 40),
                TextAlign = ContentAlignment.MiddleCenter,
                Font = new Font("Microsoft YaHei", 18, FontStyle.Bold)
            };

            // 用户名输入框
            txtUsername = new TextBox
            {
                Location = new Point(100, 100),
                Size = new Size(200, 30),
                PlaceholderText = "请输入用户名"
            };

            // 密码输入框
            txtPassword = new TextBox
            {
                Location = new Point(100, 140),
                Size = new Size(200, 30),
                PasswordChar = '•',
                PlaceholderText = "请输入密码"
            };

            // 登录按钮
            btnLogin = new Button
            {
                Text = "登录",
                Location = new Point(100, 180),
                Size = new Size(90, 30),
                BackColor = Color.FromArgb(51, 122, 183),
                ForeColor = Color.White,
                FlatStyle = FlatStyle.Flat
            };
            btnLogin.Click += BtnLogin_Click;

            // 注册按钮
            btnRegister = new Button
            {
                Text = "注册",
                Location = new Point(210, 180),
                Size = new Size(90, 30),
                BackColor = Color.FromArgb(92, 184, 92),
                ForeColor = Color.White,
                FlatStyle = FlatStyle.Flat
            };
            btnRegister.Click += BtnRegister_Click;

            this.Controls.AddRange(new Control[] { 
                lblTitle, txtUsername, txtPassword, btnLogin, btnRegister 
            });
        }

        private void BtnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUsername.Text) || string.IsNullOrEmpty(txtPassword.Text))
            {
                MessageBox.Show("请输入用户名和密码！");
                return;
            }

            try
            {
                string sql = "SELECT id, username FROM users WHERE username = @username AND password = @password";
                var parameters = new MySqlParameter[]
                {
                    new MySqlParameter("@username", MySqlDbType.VarChar) { Value = txtUsername.Text },
                    new MySqlParameter("@password", MySqlDbType.VarChar) { Value = txtPassword.Text }
                };

                var dt = DBHelper.ExecuteQuery(sql, parameters);
                if (dt.Rows.Count > 0)
                {
                    // 登录成功，保存当前用户ID
                    CurrentUser.Id = Convert.ToInt32(dt.Rows[0]["id"]);
                    CurrentUser.Username = dt.Rows[0]["username"].ToString();

                    // 打开主窗体
                    var mainForm = new MainForm();
                    this.Hide();
                    mainForm.FormClosed += (s, args) => this.Close();
                    mainForm.Show();
                }
                else
                {
                    MessageBox.Show("用户名或密码错误！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"登录失败：{ex.Message}");
            }
        }

        private void BtnRegister_Click(object sender, EventArgs e)
        {
            var registerForm = new RegisterForm();
            registerForm.ShowDialog();
        }
    }
} 